package com.flyqiu.form.core;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyqiu.common.dict.FlyQiuDicData;
import com.flyqiu.form.api.service.FlyQiuTableConfigService;
import com.flyqiu.form.api.store.FlyQiuFormCoreService;
import com.flyqiu.form.api.store.FlyQiuFormStoreManage;
import com.flyqiu.form.api.store.StoreDataService;
import com.flyqiu.form.api.store.StoreTableService;
import com.flyqiu.form.model.FlyQiuTableConfigModel;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;
import com.flyqiu.form.model.enums.FlyQiuFormDeploymentStatus;

import java.util.List;

public class FlyQiuFormCoreServiceImpl implements FlyQiuFormCoreService {


    private final FlyQiuTableConfigService flyQiuTableConfigService;
    private final FlyQiuFormStoreManage formStoreManage;

    public FlyQiuFormCoreServiceImpl(FlyQiuTableConfigService flyQiuTableConfigService, FlyQiuFormStoreManage formStoreManage) {
        this.flyQiuTableConfigService = flyQiuTableConfigService;
        this.formStoreManage = formStoreManage;
    }

    @Override
    public Page<FlyQiuTableConfig> page(Page<FlyQiuTableConfig> page, Wrapper<FlyQiuTableConfig> wrapper) {
        return flyQiuTableConfigService.page(page, wrapper);
    }

    @Override
    public List<FlyQiuTableConfig> list(Wrapper<FlyQiuTableConfig> wrapper) {
        return flyQiuTableConfigService.list(wrapper);
    }

    /**
     * 创建表单
     *
     * @param config          表单配置
     * @param synchronization 是否同步到数据库
     */
    public void createForm(FlyQiuTableConfig config, boolean synchronization) {
        StoreTableService tableEngineService = getTableEngineService(config);
        tableEngineService.createTable(config);
        if (synchronization) {
            deploymentTableById(config.getId());
        }
    }

    /**
     * 修改表
     *
     * @param config          表配置
     * @param synchronization 是否同步
     */
    public void updateForm(FlyQiuTableConfig config, boolean synchronization) {
        StoreTableService tableEngineService = getTableEngineService(config);
        tableEngineService.updateTable(config);
        if (synchronization) {
            deploymentTableById(config.getId());
        }
    }

    @Override
    public void deploymentTableById(String tableId) {
        FlyQiuTableConfigModel form = getFormModelByBId(tableId);
        StoreTableService tableEngineService = getTableEngineService(form);
        tableEngineService.synchronizationTable(form);
    }


    /**
     * 获取表单
     *
     * @param tableId       表单id
     * @param loadSqlColumn 是否加载数据库中列配置信息
     * @return 表单配置详情
     */
    @Override
    public FlyQiuTableConfigModel getFormModelByBId(String tableId, boolean loadSqlColumn) {
        FlyQiuTableConfig formConfig = flyQiuTableConfigService.getById(tableId);
        if (formConfig == null) return null;
        return getTableConfigModel(formConfig, loadSqlColumn);
    }

    /**
     * 获取表单
     *
     * @param formConfig    表单配置
     * @param loadSqlColumn 是否加载数据库中的列信息
     * @return 表单配置详情
     */
    @Override
    public FlyQiuTableConfigModel getTableConfigModel(FlyQiuTableConfig formConfig, boolean loadSqlColumn) {
        StoreTableService tableEngineService = getTableEngineService(formConfig);
        return tableEngineService.getTableConfigModel(formConfig, loadSqlColumn);
    }


    @Override
    public FlyQiuTableConfigModel getFormByCode(String formCode, boolean loadSqlColumn) {
        LambdaQueryWrapper<FlyQiuTableConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FlyQiuTableConfig::getFormCode, formCode);
        queryWrapper.eq(FlyQiuTableConfig::getDeploymentStatus, FlyQiuFormDeploymentStatus.PUBLISHED.getValue());
        queryWrapper.orderByDesc(FlyQiuTableConfig::getTableVersion);
        Page<FlyQiuTableConfig> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        page = flyQiuTableConfigService.page(page, queryWrapper);
        List<FlyQiuTableConfig> records = page.getRecords();
        if (records.isEmpty()) return null;
        FlyQiuTableConfig flyQiuTableConfig = records.get(0);
        return getTableConfigModel(flyQiuTableConfig, loadSqlColumn);
    }

    /**
     * 删除表单
     *
     * @param tableId 数据表id
     */
    @Override
    public void deleteForm(String tableId) {
        FlyQiuTableConfig formConfig = flyQiuTableConfigService.getById(tableId);
        if (formConfig == null) {
            return;
        }
        getTableEngineService(formConfig).deleteTable(formConfig);
    }

    /**
     * 获取数据表服务
     *
     * @param tableId 数据表id
     * @return 数据表服务
     */
    @Override
    public StoreTableService getTableEngineService(String tableId) {
        FlyQiuTableConfigModel form = getFormModelByBId(tableId);
        return getTableEngineService(form);
    }

    @Override
    public StoreTableService getTableEngineService(FlyQiuTableConfig config) {
        return formStoreManage.getStoreService(config.getDataStorageType(), true);
    }


    /**
     * 获取数据存储服务
     *
     * @param config 表单配置
     * @return 数据存储服务
     */

    @Override
    public StoreDataService getTableDataEngineService(FlyQiuTableConfig config) {
        return formStoreManage.getStoreService(config, true);
    }

    /**
     * 获取全部存储器
     *
     * @return 存储器字典
     */
    @Override
    public List<FlyQiuDicData> getAllStorage() {
        return formStoreManage.getStoreDict();
    }
}
